<!DOCTYPE html>
<html lang="en">
<head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form id="stuAddForm">
    <p>姓名：<input name="stuName"></p>
    <p>生日：<input name="stuBir"></p>
    <p>性别：<input name="stuSex"></p>
    <p>年龄：<input name="stuAge"></p>
    <p>班级：<select name="cid" id="claSelect">
        <option value="1">xxx班级</option>
    </select></p>
    <p>：<input name="cname"></p>
    <p><input type="button" value="提交" id="stuAddBtn"></p>
</form>
<script>
    $("#stuAddBtn").click(function (){
        $.ajax({
            url:'/student/add',
            data:$("#stuAddForm").serialize(),
            dataType: 'json',
            success:function (data){
                console.log(data)
                if(data.code==20000){
                    location.href='/views/student.html'
                }
                if(data.code==40000){
                    alert("添加失败")
                }
            }

        })
    })
    function getClassesList(){
        $.ajax({
            url:'/student/list',
            dataType:'json',
            success:function (data){
                console.log(data)
                $("#claSelect").empty()
                $.each(data.obj.list,function (index,cla){
                    $("#claSelect").append('<option value="'+cla.classes.cid+'">'+cla.classes.cname+'</option>')
                })
            }
        })
    }
    //页面预加载--页面在缓存中组织数据是，预加载就开始执行
    $(function (){
        //获取班级数据--函数调用
        getClassesList();
    })
</script>
</body>
</html>